package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.ManageDto;
import com.itheima.pojo.Manage;
import com.itheima.pojo.ManageProject;
import com.itheima.pojo.Project;
import com.itheima.service.ManageProjectService;
import com.itheima.service.ManageService;
import com.itheima.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/manage")
@Slf4j
public class ManageController {

    @Autowired
    private ManageService manageService;
    @Autowired
    private ManageProjectService manageProjectService;
    @Autowired
    private ProjectService projectService;

    /**
     * 分页查询
     * @param map
     * @return
     */
    @GetMapping("/page")
    public R<Page<Manage>> page(@RequestParam Map<String,String> map){

        String currentPage = map.get("currentPage");
        String pageSize = map.get("pageSize");
        String queryString = map.get("queryString");
        if ("null".equals(queryString)){
            queryString = null;
        }

        Page<Manage> managePage = new Page<>(Integer.parseInt(currentPage),Integer.parseInt(pageSize));

        LambdaQueryWrapper<Manage> mlqw = new LambdaQueryWrapper<>();
        mlqw.like(queryString != null,Manage::getCode,queryString).or().like(queryString != null,Manage::getName,queryString);
        mlqw.or().like(queryString != null,Manage::getHelpCode,queryString);
        manageService.page(managePage,mlqw);
        return R.success(managePage);
    }

    /**
     * 添加功能
     * @param manage
     * @return
     */
    @PostMapping("/add")
    @Transactional
    public R<String> add(@RequestParam("ids") List<Long> ids, @RequestBody Manage manage){
        manageService.save(manage);
        Long manageId = manage.getId();

        List<Project> projects = projectService.listByIds(ids);

        List<ManageProject> manageProjects = projects.stream().map(item -> {
            ManageProject manageProject = new ManageProject();
            manageProject.setManageId(manageId);
            manageProject.setProjectId(item.getId());
            return manageProject;
        }).collect(Collectors.toList());

        manageProjectService.saveBatch(manageProjects);
        return R.success("添加成功");
    }

    /**
     * 修改
     * @param ids
     * @param manage
     * @return
     */
    @PutMapping("/update")
    @Transactional
    public R<String> update(@RequestParam("ids") List<Long> ids, @RequestBody Manage manage){
        manageService.updateById(manage);

        //删除manageProject表里该检查组的项目,再重新添加
        Long manageId = manage.getId();
        LambdaQueryWrapper<ManageProject> mplqw = new LambdaQueryWrapper<>();
        mplqw.eq(ManageProject::getManageId,manageId);
        manageProjectService.remove(mplqw);

        List<Project> projects = projectService.listByIds(ids);

        List<ManageProject> manageProjects = projects.stream().map(item -> {
            ManageProject manageProject = new ManageProject();
            manageProject.setManageId(manageId);
            manageProject.setProjectId(item.getId());
            return manageProject;
        }).collect(Collectors.toList());

        manageProjectService.saveBatch(manageProjects);
        return R.success("修改成功");
    }

    @DeleteMapping("/delete/{id}")
    @Transactional
    public R<String> delete(@PathVariable Long id){

        LambdaQueryWrapper<ManageProject> mplqw = new LambdaQueryWrapper<>();
        mplqw.eq(ManageProject::getManageId,id);

        manageProjectService.remove(mplqw);
        manageService.removeById(id);
        return R.success("删除成功");
    }
}
